<template>
  <input type="text" id="songName" value="" />
  <button @click="getUrl">获取url</button>
  <button @click="postDb">送到数据库</button>
</template>

<script>
import axios from "axios";

export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  data() {
    return {
      // songName:''
      songUrl: "",
      songmid: "",
    };
  },
  methods: {
    getUrl() {
      let _this = this;
      // console.log(document.getElementById("songName").value);
      let keywords = document.getElementById("songName").value;
      let url = `/api/soso/fcgi-bin/client_search_cp?aggr=1&cr=1&flag_qc=0&p=1&n=20&w=${keywords}`;
      axios({
        method: "get",
        url: url,
      }).then(function (res) {
        // console.log(res.data);
        var strD = res.data.replace("callback(", "");
        var str = strD.substring(0, strD.length - 1);
        var strF = JSON.parse(str);
        // console.log(strF)
        _this.songmid = strF.data.song.list[0].songmid;
        // console.log("songmid", _this.songmid);
        _this.concatUrl();
      });
    },

    concatUrl() {
      let _this = this;
      let url2 =
        `/dev/cgi-bin/musicu.fcg?g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0&data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"8348972662","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"8348972662","songmid":["${this.songmid}"],"songtype":[1],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}&realIP=192.168.65.253`.trim();
      axios({
        method: "get",
        url: url2,
      }).then(function (res) {
        // console.log("concatUrl", res.data);
        let c1 = res.data.req.data.freeflowsip[0];
        let c2 = res.data.req_0.data.midurlinfo[0].purl;
        let c3 = c1.concat(c2);
        console.log(document.getElementById("songName").value, c3);
        _this.songUrl = c3;
      });
    },

    postDb() {
      // console.log(this.songUrl)
      // console.log(document.getElementById('audio'))
      let url = "http://192.168.37.29:8666/game/save/music";
      let params = {
        musicName: document.getElementById("songName").value,
        songmid: this.songmid,
      };
      axios({
        method: "post",
        url: url,
        headers: {
          "Api-Token":
            "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjE0NzgyODAzMTY5OTI0NTg3NTMiLCJleHAiOjE2NzMzMzAxOTEsImlhdCI6MTY0MTc5NDE5MX0.fsGVHe4NEgSmWXAjm9p8JRhULu4s4Yj3KVX0CaX_PEQ",
        },
        data: params,
      }).then(function (res) {
        console.log(res.data);
      });
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
